public class work1 {
    public static int totalFruit(int[] fruits) {
        int []arr=new int[fruits.length+1];
        int ret=0;int max=0;
        int left=0,right=0;
        int []temp=new int[2];
        while(right<fruits.length){
            if(arr[fruits[right]]==0){
                if(ret==0){
                    temp[0]=right;
                }
                else if(ret==1){
                    temp[1]=right;
                }else {
                    temp[0]=temp[1];
                    temp[1]=right;
                }
                ret++;
            }
            arr[fruits[right]]++;
            if(ret>2){
                int temp1=temp[0];
                int temp2=temp[1];
                while(left<right&&arr[fruits[temp1]]>0&&arr[fruits[temp2]]>0){
                    arr[fruits[left++]]--;
                }
                ret--;
            }
            max=Math.max(max,right-left+1);
            right++;
        }
        return max;
    }

    public static void main(String[] args) {
        int[]arr={1,0,1,4,1,4,1,2,3};
        System.out.println(totalFruit(arr));
    }
}
